#import "UIColor+String.h"
int convertToInt(char c)
{
    if (c >= '0' && c <= '9') {
        return c - '0';
    } else if (c >= 'a' && c <= 'f') {
        return c - 'a' + 10;
    } else if (c >= 'A' && c <= 'F') {
        return c - 'A' + 10;
    } else {
        return printf("Characters are illegal!");
    }
}
@implementation UIColor (String)
+ (UIColor *)colorWithHexString:(NSString *)name
{
    if (![[name substringToIndex:0] isEqualToString:@"#"] && name.length < 7) {
        return nil;
    }
    const char *var_str = [[name substringWithRange:NSMakeRange(1, 6)] UTF8String];
    NSString *var_alphaString = [name substringFromIndex:7];
    CGFloat var_red = (convertToInt(var_str[0])*16 + convertToInt(var_str[1])) / 255.0f;
    CGFloat var_green = (convertToInt(var_str[2])*16 + convertToInt(var_str[3])) / 255.0f;
    CGFloat var_blue = (convertToInt(var_str[4])*16 + convertToInt(var_str[5])) / 255.0f;
    CGFloat var_alpha = [var_alphaString isEqualToString:@""] ? 1 : var_alphaString.floatValue/255;
    return [UIColor colorWithRed:var_red green:var_green blue:var_blue alpha:var_alpha];
}
+ (UIColor *)colorWithHexString:(NSString *)name alpha:(CGFloat)alpha
{
    if (![[name substringToIndex:0] isEqualToString:@"#"] && name.length < 7) {
        return nil;
    }
    const char *var_str = [[name substringWithRange:NSMakeRange(1, 6)] UTF8String];
    CGFloat var_red = (convertToInt(var_str[0])*16 + convertToInt(var_str[1])) / 255.0f;
    CGFloat var_green = (convertToInt(var_str[2])*16 + convertToInt(var_str[3])) / 255.0f;
    CGFloat var_blue = (convertToInt(var_str[4])*16 + convertToInt(var_str[5])) / 255.0f;
    return [UIColor colorWithRed:var_red green:var_green blue:var_blue alpha:alpha];
}
+ (void)getRed:(CGFloat *)rPtr green:(CGFloat *)gPtr blue:(CGFloat *)bPtr fromColor:(UIColor *)color {
#if TARGET_OS_IPHONE
    BOOL var_done = NO;
    if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
        var_done = [color getRed:rPtr green:gPtr blue:bPtr alpha:NULL];
    }
    if (!var_done) {
        CGColorSpaceRef var_rgbColorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char var_pixel[4];
        CGContextRef var_context = CGBitmapContextCreate(&var_pixel, 1, 1, 8, 4, var_rgbColorSpace, (CGBitmapInfo)(kCGBitmapAlphaInfoMask & kCGImageAlphaNoneSkipLast));
        CGContextSetFillColorWithColor(var_context, [color CGColor]);
        CGContextFillRect(var_context, CGRectMake(0, 0, 1, 1));
        if (rPtr) {
            *rPtr = var_pixel[0] / 255.0f;
        }
        if (gPtr) {
            *gPtr = var_pixel[1] / 255.0f;
        }
        if (bPtr) {
            *bPtr = var_pixel[2] / 255.0f;
        }
        CGContextRelease(var_context);
        CGColorSpaceRelease(var_rgbColorSpace);
    }
#elif defined(DD_CLI) || !__has_include(<AppKit/NSColor.h>)
    [color getRed:rPtr green:gPtr blue:bPtr alpha:NULL];
#else 
    NSColor *var_safeColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
    [var_safeColor getRed:rPtr green:gPtr blue:bPtr alpha:NULL];
#endif 
}
+(UIColor *)getColorWithFromColor:(UIColor *)fromColor finalColor:(UIColor *)finalColor andRatio:(CGFloat)ratio
{
    CGFloat var_theAlpha = 0;
    if ([finalColor isEqual:fromColor]) {
        return  finalColor;
    }
    if ([finalColor isEqual:[UIColor clearColor]]) {
        var_theAlpha = 1;
       return  finalColor = [fromColor colorWithAlphaComponent:(1 - ratio)];
    }
    if ([fromColor isEqual:[UIColor clearColor]]) {
        var_theAlpha = 2;
        return  finalColor = [finalColor colorWithAlphaComponent:ratio];
    }
    CGFloat var_preRed,var_preGreen,var_preBlue;
    CGFloat var_lastRed,var_lastGreen,var_lastBlue;
    [UIColor getRed:&var_preRed green:&var_preGreen blue:&var_preBlue fromColor:fromColor];
    [UIColor getRed:&var_lastRed green:&var_lastGreen blue:&var_lastBlue fromColor:finalColor];
    CGFloat var_Rcolor = var_lastRed - var_preRed;
    CGFloat var_Gcolor = var_lastGreen - var_preGreen;
    CGFloat var_Bcolor = var_lastBlue - var_preBlue;
    return [UIColor colorWithRed:(var_preRed *255 + var_Rcolor*255*ratio)/255.0 green:(var_preGreen*255 + var_Gcolor*255*ratio)/255.0 blue:(var_preBlue*255 + var_Bcolor*255*ratio)/255.0 alpha:(var_theAlpha==0?1:var_theAlpha==1?(1-ratio):ratio)];
}
+ (instancetype)randomColor
{
    return [UIColor colorWithRed:arc4random_uniform(256) green:arc4random_uniform(256) blue:arc4random_uniform(256)];
}
+ (instancetype)colorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue
{
    return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
}
@end
